/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package GestionRegistros;

import InterfazGeneral.SystemFacade;
import Navegacion.Pagina;
import Navegacion.Paginas;
import java.io.IOException;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 *
 * @author Administrador
 */
public class GestorRegistros extends HttpServlet {
   
    /** 
     * Processes requests for both HTTP <code>GET</code> and <code>POST</code> methods.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException, SQLException {

        SystemFacade sf = SystemFacade.getInstancia();

        //PROCESAR ABM USUARIOS
        if (request.getParameter("ok") != null) {
            String nombre = request.getParameter("nombre");
            String apellido = request.getParameter("apellido");
            String email = request.getParameter("email");
            String clave = request.getParameter("clave");
            String clave2 = request.getParameter("clave2");
            ArrayList<String> errores = sf.validarUsuario(nombre, apellido, email, clave,clave2);
            if (errores.size() > 0) {
                String erroreshtml = "";
                for (Iterator<String> it = errores.iterator(); it.hasNext();) {
                    erroreshtml += "<li>" + it.next() + "</li>";
                }
                Pagina.aviso("Se hallaron los siguientes errores:", "<ul>" + erroreshtml + "</ul>", request, response);
            }
            sf.crear(nombre, apellido, email, clave);
            Pagina.aviso("El usuario " + nombre + " " + apellido + " ha sido creado.", "Ya puedes iniciar sesión con los datos suministrados.", request, response);
        }

        //DIRECCIONAMIENTO
        if (request.getParameter("nuevo") != null) {
            request.setAttribute("form_action", Paginas.REGISTRAR.getUrl());
            request.getRequestDispatcher(Paginas.REGISTRARFORM.getUrl()).forward(request, response);
        }
        



    } 

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /** 
     * Handles the HTTP <code>GET</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        try {
            processRequest(request, response);
        } catch (SQLException ex) {
            Logger.getLogger(GestorRegistros.class.getName()).log(Level.SEVERE, null, ex);
        }
    } 

    /** 
     * Handles the HTTP <code>POST</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        try {
            processRequest(request, response);
        } catch (SQLException ex) {
            Logger.getLogger(GestorRegistros.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    /** 
     * Returns a short description of the servlet.
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>

}
